//% color="#EF7060" iconWidth=30 iconHeight=30
namespace PBL{
    //% block=" [ORI_TEXT] 替换 [OLD_TEXT] 为 [NEW_TEXT]" blockType="reporter"
    //% ORI_TEXT.shadown="string" ORI_TEXT.defl="你好，马尔克斯"
    //% OLD_TEXT.shadown="string" OLD_TEXT.defl="马尔克斯"
    //% NEW_TEXT.shadown="string" NEW_TEXT.defl="marxmake"
    export function textReplace(parameter: any, block: any) {
        let ori_text = parameter.ORI_TEXT.code;
        let old_text = parameter.OLD_TEXT.code;
        let new_text = parameter.NEW_TEXT.code;
        Generator.addCode(`${ori_text}.replace(${old_text}, ${new_text})`);
    }

    //% block="选择并获取 [FILE_ACCESS]" blockType="reporter"
    //% FILE_ACCESS.shadow="dropdown" FILE_ACCESS.options="FILE_ACCESS"
    export function selectFile(parameter: any, block: any) {
        let file_access = parameter.FILE_ACCESS.code;
        Generator.addImport('import FileDialog');
        Generator.addCode(`FileDialog.${file_access}()`);
    }

    //% block="使用系统默认软件打开文件（For Windows） [FILE_NAME]" blockType="command"
    //% FILE_NAME.shadow="string" FILE_NAME.defl="filename.txt"
    export function openFileByDefault(parameter: any, block: any) {
        let file_name = parameter.FILE_NAME.code;
        Generator.addImport('import os');
        Generator.addCode(`os.startfile(${file_name})`);
    }

    //% block="文件对象 [FILE_OBJ] 打开文件 [FILE_NAME] 模式 [FILE_PATTERN]" blockType="command"
    //% FILE_OBJ.shadow="string" FILE_OBJ.defl="file_obj"
    //% FILE_NAME.shadow="string" FILE_NAME.defl="filename.txt"
    //% FILE_PATTERN.shadow="dropdown" FILE_PATTERN.options="FILE_PATTERN"
    export function openFile(parameter: any, block: any) {
        let file_obj = parameter.FILE_OBJ.code;
        let file_name = parameter.FILE_NAME.code;
        let file_pattern = parameter.FILE_PATTERN.code;
        Generator.addImport('import os');
        Generator.addCode(`${file_obj.slice(1,-1)} = open(${file_name}, '${file_pattern}')`);
    }

    //% block="将 [CONTEXT] 写入文件对象 [FILE_OBJ]" blockType="command"
    //% CONTEXT.shadow="string" CONTEXT.defl="Marxmake"
    //% FILE_OBJ.shadow="string" FILE_OBJ.defl="file_obj"
    export function writeFile(parameter: any, block: any) {
        let context = parameter.CONTEXT.code;
        let file_obj = parameter.FILE_OBJ.code;
        Generator.addImport('import os');
        Generator.addCode(`${file_obj.slice(1,-1)}.write(${context})`);
    }

    //% block="使用麦克风录制音频 文件名 [FILE_NAME] 录音时长 [SECOND] 秒" blockType="command"
    //% FILE_NAME.shadow="string" FILE_NAME.defl="d:\\1.wav"
    //% SECOND.shadow="number" SECOND.defl="3"
    export function recordAudio(parameter: any, block: any) {
        let file_name = parameter.FILE_NAME.code;
        let second = parameter.SECOND.code;
        Generator.addImport('import audio')
        Generator.addCode(`audio.audio_record(${file_name}, ${second})`);
    }

    //% block="使用摄像头拍摄照片 文件名 [FILE_NAME] 拍摄按键 [BUTTON]" blockType="command"
    //% FILE_NAME.shadow="string" FILE_NAME.defl="d:\\1.jpg"
    //% BUTTON.shadow="string" BUTTON.defl="q"
    export function takePicture(parameter: any, block: any) {
        let file_name = parameter.FILE_NAME.code;
        let button = parameter.BUTTON.code;
        Generator.addImport('import cam')
        Generator.addCode(`cam.photo_capture(${file_name}, ${button})`);
    }

    //% block="从 [JSON] 获取结果 [AI_RESULT]" blockType="reporter"
    //% JSON.shadow="string" JSON.defl="JSON"
    //% AI_RESULT.shadow="dropdown" AI_RESULT.options="AI_RESULT"
    export function processResult(parameter: any, block: any) {
        let json = parameter.JSON.code;
        let ai_result =  parameter.AI_RESULT.code;
        if (ai_result == "ImageRecognition") {
            Generator.addCode(`${json}["result"][0]["keyword"]`);
        } else if (ai_result == "SpeechRecognition") {
            Generator.addCode(`${json}["result"][0]`);
        } else if (ai_result == "FaceRecognition") {
            Generator.addCode(`${json}["score"]`);
        } else if (ai_result == "CharacterRecognition") {
            Generator.addCode(`${json}["words_result"]`);
        } else if (ai_result == "ShortTextSimilarity") {
            Generator.addCode(`${json}["score"]`);
        }
    }

    //% block="设置 APP_ID [APP_ID]" blockType="command"
    //% APP_ID.shadow="string" APP_ID.defl="Enter Your APP_ID"
    export function setAPPID(parameter: any, block: any) {
        let app_id = parameter.APP_ID.code;
        Generator.addCode(`app_id = ${app_id}`);
    }

    //% block="设置 API_KEY [API_KEY]" blockType="command"
    //% API_KEY.shadow="string" API_KEY.defl="Enter Your API_KEY"
    export function setAPIKEY(parameter: any, block: any) {
        let api_key = parameter.API_KEY.code;
        Generator.addCode(`api_key = ${api_key}`);
    }

    //% block="设置 SECRET_KEY [SECRET_KEY]" blockType="command"
    //% SECRET_KEY.shadow="string" SECRET_KEY.defl="Enter Your SECRET_KEY"
    export function setSECRETKEY(parameter: any, block: any) {
        let secret_key = parameter.SECRET_KEY.code;
        Generator.addCode(`secret_key = ${secret_key}`);
    }

    //% block="初始化 [AI_METHOD] 客户端 [CLIENT]" blockType="command"
    //% AI_METHOD.shadow="dropdown" AI_METHOD.options="AI_METHOD"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    export function initAi(parameter: any, block: any) {
        let ai_method = parameter.AI_METHOD.code;
        let client = parameter.CLIENT.code;
        Generator.addImport('import aip');
        Generator.addCode(`${client.slice(1,-1)} = aip.${ai_method}(app_id, api_key, secret_key)`);
    }

    //% block="语音合成 客户端 [CLIENT] 内容 [CONTEXT] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% CONTEXT.shadow="string" CONTEXT.defl="今天天气真好"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function speechSynthesis(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let context = parameter.CONTEXT.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.synthesis(${context}, options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.synthesis(${context}, options={})`)
        }
    }


    //% block="语音识别 客户端 [CLIENT] 文件对象 [FILE_OBJ] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% FILE_OBJ.shadow="string" FILE_OBJ.defl="file_obj"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function speechRecognition(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let file_obj = parameter.FILE_OBJ.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.asr(${file_obj.slice(1,-1)}.read(), options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.asr(${file_obj.slice(1,-1)}.read(), options={})`)
        }
    }

    //% block="图像识别 客户端 [CLIENT] 功能 [IMG_SCENE] 文件对象 [FILE_OBJ] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% IMG_SCENE.shadow="dropdown" IMG_SCENE.options="IMG_SCENE"
    //% FILE_OBJ.shadow="string" FILE_OBJ.defl="file_obj"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function imageRecognition(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let img_scene = parameter.IMG_SCENE.code;
        let file_obj = parameter.FILE_OBJ.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.${img_scene}(${file_obj.slice(1,-1)}.read(), options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.${img_scene}(${file_obj.slice(1,-1)}.read(), options={})`)
        }
    }

    //% block="人脸比对 客户端 [CLIENT] 图片1路径 [IMG_PATH_A] 图片2路径 [IMG_PATH_B] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% IMG_PATH_A.shadow="string" IMG_PATH_A.defl="d:\\marxmake.jpg"
    //% IMG_PATH_B.shadow="string" IMG_PATH_B.defl="d:\\marxmake.jpg"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function faceContrast(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let img_path_a = parameter.IMG_PATH_A.code;
        let img_path_b = parameter.IMG_PATH_B.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.match(${img_path_a}, ${img_path_b}, options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.match(${img_path_a}, ${img_path_b}, options={})`)
        }
    }

    //% block="文字识别 客户端 [CLIENT] 功能 [OCR_SCENE] 文件对象 [FILE_OBJ] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% OCR_SCENE.shadow="dropdown" OCR_SCENE.options="OCR_SCENE"
    //% FILE_OBJ.shadow="string" FILE_OBJ.defl="file_obj"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function characterRecognition(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let ocr_scene = parameter.OCR_SCENE.code;
        let file_obj = parameter.FILE_OBJ.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.${ocr_scene}(${file_obj.slice(1,-1)}.read(), options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.${ocr_scene}(${file_obj.slice(1,-1)}.read(), options={})`)
        }
    }

    //% block="自然语言处理 客户端 [CLIENT] 功能 [NLP_SCENE] 内容 [CONTENT] 属性 [OPTIONS]" blockType="reporter"
    //% CLIENT.shadow="string" CLIENT.defl="client"
    //% NLP_SCENE.shadow="dropdown" NLP_SCENE.options="NLP_SCENE"
    //% CONTENT.shadow="string" CONTENT.defl="马尔克斯欢迎您"
    //% OPTIONS.shadow="string" OPTIONS.defl="空字典"
    export function naturalLanguage(parameter: any, block: any) {
        let client = parameter.CLIENT.code;
        let nlp_scene = parameter.NLP_SCENE.code;
        let content = parameter.CONTENT.code;
        let options = parameter.OPTIONS.code;
        if (options != '"空字典"') {
            Generator.addCode(`${client.slice(1,-1)}.${nlp_scene}(${content}, options=${options.slice(1,-1)})`)
        } else {
            Generator.addCode(`${client.slice(1,-1)}.${nlp_scene}(${content}, options={})`)
        }
    }
}